import { updatePoint } from "./updatepoint"
import { docLayer } from "../../Map/Hooks/useGuangguMap"
export const EventUpdate = (map) => {
  var position = ref()
  const layerId = ref()
  function monitor(index) {
    //  index 图层id
    layerId.value = index
    // 点击事件
    map.value.on("click", handleMap)
  }
  // 关闭popup弹窗
  function popuShow() {
    //移除点击事件
    map.value.un("click", handleMap)
    feature.value = ""
    update.value = ""
  }
  // 点击事件回调函数
  function handleMap(e) {
    position.value = e.coordinate
    // 调用封装点查询
    Point.query({
      position: position.value,
      service: {
        name: "guanggu",
        layerId: layerId.value,
      },
      callback: onSuccess,
    })
  }
  // popup弹窗
  const clientX = ref()
  const clientY = ref()
  var feature = ref()
  var update = ref(false)
  // 事件更新 数据变量
  var incidentNumber = ref()
  var incidentType = ref()
  var incidentLevel = ref()
  var incidentTime = ref()
  var incidentAddress = ref()
  var incidentCar = ref()
  var incidentPerson = ref()
  var ProcessingState = ref()
  var fid = ref()
  var arr
  function onSuccess(res) {
    // 判断是否有监控点，如果有popup弹窗
    if (res) {
      arr = res[0].values_.values_
      fid.value = res[0].id_
      update.value = true

      incidentNumber.value = arr.事件编号
      incidentType.value = arr.事件类型
      incidentLevel.value = arr.事件等级
      incidentTime.value = arr.发生时间
      incidentAddress.value = arr.发生地点
      incidentCar.value = arr.车牌号
      incidentPerson.value = arr.驾驶员
      ProcessingState.value = arr.处理状态
      // 经纬度坐标转屏幕坐标
      var lnglat = map.value.getPixelFromCoordinate(position.value)
      clientX.value = lnglat[0] + "px"
      clientY.value = lnglat[1] + "px"
    }
  }
  // 更新数据
  function updatePoint1() {
    map.value.un("click", handleMap)
    // 定义要修改属性的字段
    var attr = [
      { key: "处理状态", value: Number(ProcessingState.value), type: "number" },
    ]
    updatePoint({ fid, attr, docLayer })
    update.value = false
  }
  return {
    monitor,
    updatePoint1,
    popuShow,
    update,
    clientY,
    clientX,
    incidentNumber,
    incidentType,
    incidentLevel,
    incidentTime,
    incidentAddress,
    incidentCar,
    incidentPerson,
    ProcessingState,
  }
}
